package jgl.vajra.management.widget.manager.retrofit;

import jgl.vajra.management.widget.retrofithelper.http.ApiServer;
import jgl.vajra.management.widget.retrofithelper.http.Url;
import jgl.vajra.management.widget.utils.CommonUtils;
import jgl.vajra.management.widget.utils.FileUtils;
import jgl.vajra.management.widget.retrofithelper.Tls12SocketFactory;
import jgl.vajra.management.widget.retrofithelper.interceptor.CacheOfflineInterceptor;
import jgl.vajra.management.widget.retrofithelper.interceptor.CacheOnlineInterceptor;
import jgl.vajra.management.widget.retrofithelper.interceptor.HttpNativeStatusInterceptor;
import jgl.vajra.management.widget.retrofithelper.interceptor.NetworkInterceptor;
import jgl.vajra.management.widget.retrofithelper.interceptor.RetryInterceptor;
import jgl.vajra.management.widget.retrofithelper.interceptor.SecurityInterceptor;
import jgl.vajra.management.widget.retrofithelper.rxexception.TokenInvalidInterceptor;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class RetrofitJsonManagerTest extends RetrofitManager {


    private static class SingleHttpMethods {
        private static final RetrofitJsonManagerTest INSTANCE = new RetrofitJsonManagerTest();
    }

    //获取单例
    public static RetrofitJsonManagerTest getInstance() {
        return SingleHttpMethods.INSTANCE;
    }

    private RetrofitJsonManagerTest() {

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, null, new SecureRandom());
            SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
            httpClientBuilder.sslSocketFactory(socketFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //添加头部
        httpClientBuilder.addInterceptor(new SecurityInterceptor());
        //token过期拦截
        httpClientBuilder.addInterceptor(new TokenInvalidInterceptor());
        //http状态码拦截
        httpClientBuilder.addInterceptor(new HttpNativeStatusInterceptor());
        //重试次数
        httpClientBuilder.addInterceptor(new RetryInterceptor(3));
        // 有网络的情况下,设置缓存
        httpClientBuilder.addNetworkInterceptor(new CacheOnlineInterceptor());
        // 没有网络的清下,设置缓存
        httpClientBuilder.addInterceptor(new CacheOfflineInterceptor());
        //声明缓存地址和大小
        Cache cache = new Cache(FileUtils.getDiskNetCacheFolderFile(), 50 * 1024 * 1024);
        httpClientBuilder.cache(cache);
        if (!CommonUtils.isAppRelease()) {


            NetworkInterceptor networkInterceptor = new NetworkInterceptor();
            networkInterceptor.setLevel(NetworkInterceptor.Level.BODY);
            httpClientBuilder.addInterceptor(networkInterceptor);
        }

        httpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
        httpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
        httpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
        httpClientBuilder.retryOnConnectionFailure(true);
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(Url.BASE_URL2)
                .client(httpClientBuilder.build())
                .build();
        apiService = retrofit.create(ApiServer.class);
    }


}
